package org.spiderflow.core.utils;

import org.spiderflow.core.model.SplitText;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class StringUtils {

    public static SplitText.TextType checkSpecialSplitChar(String content){
        List<String> result = new ArrayList<>();
        int start = 0;
        int findSingleCount = 0;
        int findDoubleCount = 0;
        boolean firstFind = false;
        SplitText.TextType retType = SplitText.TextType.COMMON;
        for(int i=0;i<content.length();i++){
            if(content.charAt(i) == '"') {
                if (!firstFind) {
                    firstFind = true;
                    retType = SplitText.TextType.DOUBLE;
                }
                findDoubleCount += 1;
            }
//            }else if(content.charAt(i) == '\''){
//                if(!firstFind){
//                    firstFind = true;
//                    retType = SplitText.TextType.SINGLE;
//                }
//                findSingleCount += 1;
//            }
        }
        if(findDoubleCount%2 != 0) {
            retType = SplitText.TextType.COMMON;
        }
//        }else if(findSingleCount >= 1){
//            retType = SplitText.TextType.COMMON;
//        }
        return retType;
    }

    public static boolean judeSentenceCanSplit(String content,char splitChar,String regexStr){
        List<String> result = new ArrayList<>();
        int start = -1;
        SplitText.TextType retType = SplitText.TextType.COMMON;
        String subContent = "";
        for(int i=0;i<content.length();i++){
            if(content.charAt(i) == splitChar){
                if(start !=-1 && start != i){
                    subContent = content.substring(start,i+1);
                    break;
                }
               start = i;
            }
        }
        Pattern p = Pattern.compile(regexStr);
        String[] strs = p.split(subContent);
        if(strs.length>1){
            return true;
        }else{
            return false;
        }
    }
}
